using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using XFGameFramework.Localization;

namespace XFGameFramework.Localization
{
    public static class YoudaoTranslante
    { 
        private static readonly Dictionary<SystemLanguage, string> youdao_language = new Dictionary<SystemLanguage, string>
    {
        { SystemLanguage.Afrikaans,"af" }, { SystemLanguage.Arabic,"ar" }, { SystemLanguage.Basque,"eu" },{ SystemLanguage.Belarusian,"be" },
        { SystemLanguage.Bulgarian,"bg" }, { SystemLanguage.Catalan,"ca" },{ SystemLanguage.Chinese,"zh-CHS" },{ SystemLanguage.Czech,"cs" },
        { SystemLanguage.Danish,"da" },{ SystemLanguage.Dutch,"nl" },{ SystemLanguage.English,"en" },{ SystemLanguage.Estonian,"et" },
        { SystemLanguage.Faroese,"" }, { SystemLanguage.Finnish,"fi" }, { SystemLanguage.French,"fr" },{ SystemLanguage.German,"de" },
        { SystemLanguage.Greek,"el" },{ SystemLanguage.Hebrew,"he" },{ SystemLanguage.Icelandic,"is" },{ SystemLanguage.Indonesian,"id" },
        { SystemLanguage.Italian,"it" },{ SystemLanguage.Japanese,"ja" },{ SystemLanguage.Korean,"ko" },{ SystemLanguage.Latvian,"lv" },
        { SystemLanguage.Lithuanian,"lt" }, { SystemLanguage.Norwegian,"no" },{ SystemLanguage.Polish,"pl" },{ SystemLanguage.Portuguese,"pt" },
        { SystemLanguage.Romanian,"ro" }, { SystemLanguage.Russian,"ru" }, { SystemLanguage.SerboCroatian,"hr" },{ SystemLanguage.Slovak,"sk" },
        { SystemLanguage.Slovenian,"sl" },{ SystemLanguage.Spanish,"es" },{ SystemLanguage.Swedish,"sv" },{ SystemLanguage.Thai,"th" },
        { SystemLanguage.Turkish,"tr" },{ SystemLanguage.Ukrainian,"uk" },{ SystemLanguage.Vietnamese,"vi" },{ SystemLanguage.ChineseSimplified,"zh-CHS" },
        { SystemLanguage.ChineseTraditional,"zh-CHT" }, { SystemLanguage.Hungarian,"hu" },
    };

        public static UnityWebRequest Translate(string content, SystemLanguage from, SystemLanguage to, string vocabId = "")
        { 
            string f = string.Empty;
            if (youdao_language.ContainsKey(from))
                f = youdao_language[from];

            string t = string.Empty;
            if (youdao_language.ContainsKey(to))
                t = youdao_language[to];
              
            return Translate(content, f, t, vocabId);
        }
         
        private static UnityWebRequest Translate(string content, string from, string to, string vocabId = "")
        {
            Dictionary<string, string> param = new Dictionary<string, string>();
             
            param.Add("q", content);
            param.Add("from", from);
            param.Add("to", to);
            param.Add("vocabId", vocabId);

            string app_id = LocalizationSettings.Instance.youdao.app_id;
            string secret_key = LocalizationSettings.Instance.youdao.secret_key;

            // 添加鉴权相关参数
            AuthV3Util.addAuthParams(app_id, secret_key, param);
            Dictionary<String, String> header = new Dictionary<string, string>() { { "Content-Type", "application/x-www-form-urlencoded" } };
            // 请求api服务 
            return Post("https://openapi.youdao.com/api", header, param, "application/json");
        }


        private static UnityWebRequest Post(string url, Dictionary<String, String> header, Dictionary<String, String> param, string expectContentType)
        {
            StringBuilder content = new StringBuilder();

            if (param != null)
            {
                int i = 0;
                foreach (var key in param.Keys)
                {
                    if (i > 0)
                    {
                        content.Append("&");
                    }
                    content.AppendFormat("{0}={1}", key, UnityWebRequest.EscapeURL(param[key]));
                    i++;
                }
            }

#if UNITY_2022_1_OR_NEWER 
            UnityWebRequest request = UnityWebRequest.PostWwwForm(url,string.Empty); 
#else
            UnityWebRequest request = UnityWebRequest.Post(url,string.Empty); 
#endif


            byte[] bodyRaw = Encoding.UTF8.GetBytes(content.ToString());
            request.uploadHandler = new UploadHandlerRaw(bodyRaw);
            request.downloadHandler = new DownloadHandlerBuffer();
            request.useHttpContinue = false;
            request.disposeUploadHandlerOnDispose = true;
            request.disposeDownloadHandlerOnDispose = true;

            foreach (var item in header.Keys)
            {
                request.SetRequestHeader(item, header[item]);
            }


            request.SendWebRequest();


            return request;

        }

    }
}